package com.pasabahceambalaj.session;

import com.pasabahceambalaj.entity.*;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.framework.EntityHome;

@Name("malzemeHome")
public class MalzemeHome extends EntityHome<Malzeme> {

	@In(create = true)
	FirmaHome firmaHome;
	@In(create = true)
	KalipHome kalipHome;
	@In(create = true)
	OlcubirimHome olcubirimHome;

	public void setMalzemeId(Long id) {
		setId(id);
	}

	public Long getMalzemeId() {
		return (Long) getId();
	}

	@Override
	protected Malzeme createInstance() {
		Malzeme malzeme = new Malzeme();
		return malzeme;
	}

	public void load() {
		if (isIdDefined()) {
			wire();
		}
	}

	public void wire() {
		getInstance();

		Firma firma = null;
		if (Authenticator.isAdmin()) {
			firma = firmaHome.getDefinedInstance();
		} else {
			firma = Authenticator.getFirma();
		}
		if (firma != null) {
			getInstance().setFirma(firma);
		}

		Kalip kalip = kalipHome.getDefinedInstance();
		if (kalip != null) {
			getInstance().setKalip(kalip);
		}
		Olcubirim olcubirim = olcubirimHome.getDefinedInstance();
		if (olcubirim != null) {
			getInstance().setOlcubirim(olcubirim);
		}
	}

	public boolean isWired() {
		if (getInstance().getFirma() == null)
			return false;
		return true;
	}

	public Malzeme getDefinedInstance() {
		return isIdDefined() ? getInstance() : null;
	}

}
